﻿using System;
using SharpArch.Core.DomainModel;
using NHibernate.Validator.Constraints;
using System.Collections;
using System.Collections.Generic;

namespace Peak.Core
{
  /// <summary>
  /// Description of Task.
  /// </summary>
  public class Task : EntityWithBusinessRules
  {
    public Task()
    {
      Changes = new List<TaskChange>();
    }

    [NotNullNotEmpty]
    public virtual string Name { get; set; }
    [NotEmpty]
    public virtual string Category { get; set; }
    public virtual string Description { get; set; }
    public virtual DateTime Created { get; set; }
    public virtual string CreatedBy { get; set; }            
    public virtual User AssignedTo { get; set; }
    public virtual DateTime? Modified { get; set; }
    public virtual DateTime? StartDate { get; set; }
    public virtual DateTime? EndDate { get; set; }
    [NotNullNotEmpty]
    public virtual string ModifiedBy { get; set; }   

    [NotNull]
    public virtual SprintStory SprintStory { get; set; }

    public virtual Project Project { get; set; }
    public virtual IList<TaskChange> Changes { get; set; }

    TaskStatus _status = TaskStatus.ToDo;
    public virtual TaskStatus Status
    {
      get
      {
        return _status;
      }
      set
      {
        if (_status != value)
        {
          if(_status == TaskStatus.ToDo && StartDate == null)
          {
            StartDate = DateTime.Now;
          }

          if (_status == TaskStatus.Done && SprintStory != null)
          {
            SprintStory.OpenedTaskCount++;
          }

          if (value == TaskStatus.Done && SprintStory != null)
          {
            SprintStory.OpenedTaskCount--;
          }          

          _status = value;

          if (IsDone)
          {
            RemainingEffort = new TimeSpan();
            EndDate = DateTime.Now;
          }

          if(SprintStory != null)
          {
            SprintStory.CheckStatus();
          }          
        }
      }
    }
    
    private TimeSpan _remainingEffort;
    public virtual TimeSpan RemainingEffort
    {
      get
      {
        return _remainingEffort;
      }
      set
      {
        if (_remainingEffort != value)
        {
          if (RulesEnabled() && SprintStory != null)
          {
            SprintStory.RemainingEffort += value - _remainingEffort;
          }

          _remainingEffort = value;
        }
      }
    }

    private TimeSpan _currentEffort;
    public virtual TimeSpan CurrentEffort
    {
      get
      {
        return _currentEffort;
      }
      set
      {
        if (_currentEffort != value)
        {
          if (RulesEnabled() && SprintStory != null)
          {
            SprintStory.CurrentEffort += value - _currentEffort;
          }

          _currentEffort = value;
        }
      }
    }

    public virtual bool IsDone
    {
      get 
      { 
        return Status == TaskStatus.Done;
      }
    }

    public virtual Task Clone()
    {
      Task result = new Task();
      result._currentEffort = this._currentEffort;
      result._remainingEffort = this._remainingEffort;
      result._status = this._status;
      result.AssignedTo = this.AssignedTo;
      result.Category = this.Category;
      result.Created = this.Created;
      result.CreatedBy = this.CreatedBy;
      result.Description = this.Description;
      result.EndDate = this.EndDate;
      result.Modified = this.Modified;
      result.ModifiedBy = this.ModifiedBy;
      result.Name = this.Name;
      result.StartDate = result.StartDate;      
      return result;
    }
  }
}
